package com.syni.mdd.yhd.common.all.listen.groupMessage;

import com.syni.mdd.yhd.common.all.config.AuroraConfig;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.*;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.utils.*;
import com.syni.mdd.yhd.common.all.utils.aurora.AuroraPushUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class GroupMessageListener implements ApplicationListener<GroupMessageEvent> {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsBusinessGroupMessageService bmsBusinessGroupMessageService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Override
    @Async
    public void onApplicationEvent(GroupMessageEvent groupMessageEvent) {
        BmsBusiness business = groupMessageEvent.getBmsBusiness();
        Set<Integer> sendUserIds = groupMessageEvent.getSendUsers();
        String content = groupMessageEvent.getContent();
        //查出要发送的用户的消息
        Map<Integer,BmsDxNews> bmsDxNewsMap = bmsBusinessGroupMessageService.getBmsNewMapByUserIdAndBusinessId(business.getId(),sendUserIds);
        Date nowTime = new Date();
        if(sendUserIds.size() > 0) {
            List<BmsDxNews> bmsDxNewsList = new LinkedList<>();
            for(Integer userId : sendUserIds){
                BmsDxNews bmsDxNews;
                if(bmsDxNewsMap.containsKey(userId)){
                    bmsDxNews = bmsDxNewsMap.get(userId);
                    bmsDxNews.setTitile(business.getVendorName());
                    bmsDxNews.setContent(content);
                    bmsDxNews.setOriginRole(Constants.GENERAL_USER_ROLE_ID);
                    bmsDxNews.setUpdateTime(nowTime);
                }else {
                    bmsDxNews = new BmsDxNews();
                    bmsDxNews.setBmsUserId(userId);
                    bmsDxNews.setBmsBusinessId(business.getId());
                    bmsDxNews.setParentType(BmsDxNewsService.PARENTTYPT_PRO);
                    bmsDxNews.setChildType(BmsDxNewsService.PARENTTYPT_PRO_ORDINARY);
                    bmsDxNews.setTitile(business.getVendorName());
                    bmsDxNews.setContent(content);
                    bmsDxNews.setBusinessImg(business.getLogoUrl());
                    bmsDxNews.setNewTime(nowTime);
                    bmsDxNews.setUpdateTime(nowTime);
                    //bmsDxNews.setFromOrigin(Constants.USER_CLIENT);
                    bmsDxNews.setOriginRole(Constants.GENERAL_USER_ROLE_ID);
                }
                bmsDxNewsList.add(bmsDxNews);
            }
            bmsDxNewsService.getBaseDao().saveAll(bmsDxNewsList);
            Set<String> sendUsers = new HashSet<>();
            for(Integer userId : sendUserIds){
                sendUsers.add(AuroraConfig.USER_PREFIX + userId.toString());
            }
            //进行极光推送
            String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.USER_RECE_GROUP_MES).getAlert(),bmsDxNewsList.get(0).getTitile());
            BmsCommonService.pushToFront(bmsDxNewsList.get(0),AuroraPushCode.USER_RECE_GROUP_MES,alert,sendUsers.toArray(new String[sendUsers.size()]));
        }
    }
}
